#include "setting.h"
#include "ui_setting.h"
#include <QtSerialPort/QSerialPortInfo>
Setting::Setting(SetInfo *set, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Setting)
{
    ui->setupUi(this);
    m_set = set;

    foreach (const QSerialPortInfo info,QSerialPortInfo::availablePorts()) {

        ui->comPortName->addItem(info.portName());
        if(m_set->portName == info.portName())
        {
            ui->comPortName->setCurrentIndex(ui->comPortName->count() -1);
        }
    }
    for (int i = 0; i <ui->comBaudRate->count();i ++)
    {

        if(m_set->bardrate == ui->comBaudRate->itemText(i).toInt())
        {
            ui->comBaudRate->setCurrentIndex(i);
        }
    }

    for(int i = 0;i < ui->comParity->count(); i ++)
    {
        if(m_set->parity && m_set->parity == i+1)
        {
            ui->comParity->setCurrentIndex(i);
        }
    }


}

Setting::~Setting()
{
    delete ui;
    m_set = 0;
}

void Setting::on_buttonBox_accepted()
{
    m_set->portName = ui->comPortName->currentText();
    bool ok;
    m_set->bardrate = static_cast<QSerialPort::BaudRate>(ui->comBaudRate->currentText().toInt(&ok));
    int i = ui->comParity->currentIndex()== 0? 0:ui->comParity->currentIndex()+1;
    m_set->parity = static_cast<QSerialPort::Parity>(i);
    deleteLater();
}
